package com.x.mvp;

import android.app.Application;

import com.x.mvp.entity.common.DeviceInfo;
import com.x.mvp.utils.DeviceUtils;

import java.lang.ref.SoftReference;

public final class LocalPersistent {
    private MVPApplication application;
    private SoftReference<DeviceInfo> deviceRef;


    public static LocalPersistent newLocalPersistent(Application application) {
        return new LocalPersistent(application).init();
    }

    private LocalPersistent(Application application) {
        this.application = (MVPApplication) application.getApplicationContext();
    }

    private LocalPersistent init() {
        deviceRef = new SoftReference<>(
                DeviceUtils.getDeviceInfo(application));
        return this;
    }

    public DeviceInfo getDeviceInfo() {
        DeviceInfo device = deviceRef.get();
        if (device == null) {
            device = DeviceUtils.getDeviceInfo(application);
            deviceRef = new SoftReference<>(device);
        }
        return device;
    }
}